package cc.vv.baselibrary.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.CallSuper;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import cc.vv.baselibrary.bean.PublicViewHolder;
import cc.vv.baselibrary.util.BaseResourceUtil;

/**
 * 公共Activity基类<p>
 *
 * @author sun.li
 */
public abstract class BaseActivity extends UtimingBaseActivity {

    @CallSuper
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(initLayoutRes());
        initViewHolder(initViewHolderObject());
        super.onCreate(savedInstanceState);
    }

    /**
     * 初始化界面布局文件
     */
    protected abstract int initLayoutRes();

    /**
     * 初始化界面资源对象文件
     */
    protected abstract PublicViewHolder initViewHolderObject();

    /**
     * 初始化界面资源对象文件
     */
    protected abstract void baseOnClick(int viewId);

    @CallSuper
    @Override
    protected void initView(@Nullable Bundle bundle) {
        super.initView(bundle);
    }

    /**
     * 初始化界面中所有控件
     */
    private void initViewHolder(PublicViewHolder viewHolder) {
        BaseResourceUtil.getInstance().initViewHolderResource(this, viewHolder,
                new BaseResourceUtil.ViewEventInterface() {
                    @Override
                    public void viewOnClick(int resId) {
                        baseOnClick(resId);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 获取点击事件
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 判定是否需要隐藏
     *
     * @param v
     * @param ev
     * @return
     */

    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 隐藏软键盘
     */
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
